package com.yunpuvip.sachet.core.tag;

import cn.hutool.core.util.StrUtil;
import org.beetl.core.GeneralVarTagBinding;
import org.beetl.core.Tag;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.awt.*;
import java.io.IOException;
import java.util.Map;

/**
 * @ClassName BeetlTags
 * @Description TODO
 * @Autor wushaohong
 * @Date 2019/3/11
 * @Version 1.0
 */
@Component
@TagName("pic")
public class PicBeetlTag extends GeneralVarTagBinding {


    @Override
    public void render() {
        Map arg = (Map) this.args[1];
        String name = (String) arg.get("name");
        String require = (String) arg.get("require")==null? "":"require";
        String hiddenValue = (String) arg.get("hiddenValue");
        String hiddenInput = (String) arg.get("hiddenInput");
        String uploadButton = (String) arg.get("uploadButton");
        String avaterImg = (String) arg.get("avaterImg");
        String id = (String) arg.get("id");
        String boxid=(String) arg.get("boxid");
        String imgName=(String) arg.get("imgName");
        String tip=(String) arg.get("tip");
        String tipCss= "";
        String defaultImg="../assets/common/images/default.jpg";

        //图片数据
        if(StrUtil.isNotEmpty(avaterImg)){
            defaultImg=avaterImg;
        }
        //是否屏蔽图片按钮
        String uploadBtnCss="<button type=button class=\"layui-btn layui-btn-radius layui-btn-warm\" id=\""+uploadButton+"\"><i class=\" layui-icon layui-icon-picture\"></i>上传</button>\n";
        if(StrUtil.isEmpty(uploadButton)){
            uploadBtnCss="";
        }
        //图片提示
        if(StrUtil.isNotEmpty(tip)){
                tipCss="<div><span style='color: red;'>"+tip+"</span></div>";
        }

        String css=
                "<div class=\"layui-form-item layui-col-md4 layui-col-lg3 \">\n" +
                "        <label class=\"layui-form-label\">"+name+"<span style=\"color: red;\">*</span></label>\n" +
                "        <div class=\"layui-input-block\" id=\""+boxid+"\" >\n" +
                "            <div class=\"layui-img\" id=\"layerPhoto\" style=\"display: inline-block;  \">\n" +
                "                <img id=\""+id+"\" layer-src=\""+defaultImg+"\"  name=\""+imgName+"\"  src=\""+defaultImg+"\"    style=\"height: 100px;width: 100px;\"/>\n" +
                "            </div>\n" +
                "            <input type=\"hidden\" name=\""+hiddenInput+"\"  value=\""+hiddenValue+"\" id=\""+hiddenInput+"\" >\n" +
                        uploadBtnCss+tipCss+
                "        </div>\n" +
                "    </div>";



        try {
            this.ctx.byteWriter.writeString(css);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }


}
